package Servlet;

import Dao.CommentDao;
import Vo.Post;
import Vo.User;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "commentServlet", value = "/commentServlet")
public class commentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("成功连接到Servlet！");
        String comment =request.getParameter("comment");
        System.out.println(comment);
        HttpSession session=request.getSession();
        Post post=(Post) session.getAttribute("post");
        User user=(User)session.getAttribute("user");
        comment=new String(comment.getBytes("ISO-8859-1"));
        CommentDao commentDao=new CommentDao();
        try {
            commentDao.Comment(post.getPid(), post.getUid(), comment, user.getUid());
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("home.jsp");

    }
}
